module CoolMultiSelect
  module ViewHelper
    def cool_multi_select(name, option_list, option_value, option_name, select_title)
      option_list = [] if !option_list
      select_title = "Select" if !select_title
      s = ""
      s += <<-EOS
        <input type="text" class="cool_multi_select" onmousedown="$(this).next().next().show();" onfocus="$(this).next().next().show();" />
        <a class="cool_multi_select" href="#" onclick="$(this).next().show();" ></a>
        <div class="cool_multi_select" style="display: none">
          <div class="cool_multi_select_content">
            <table class="cool_table">
              <tr class="cool_header">
                <td style="width: 25px;"><input type="checkbox" name="all" onclick="cool_select_all(this);" /></td>
                <td>#{select_title}</td>
              </tr>
      EOS
      option_list.each_with_index do |m, i|
        tr_class = "cool_even" if i % 2 == 0
        tr_class = "cool_odd" if i % 2 == 1
        s += <<-EOS
          <tr class="#{tr_class}" onmouseover="$(this).addClass('highlight');" onmouseout="$(this).removeClass('highlight');">
            <td><input type="checkbox" onclick="cool_select_one(this);" /><input type="hidden" name="#{name}" value="#{m.send(option_value)}" /></td>
            <td>#{m.send(option_name)}</td>
          </tr>
        EOS
      end
      s += <<-EOS  
            </table>
          </div>
          <div class="cool_ok">
            <button onclick="cool_ok(this);">OK</button>
          </div>
        </div>
      EOS
      s
    end
  end
end